using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace CPRMulti.Tools
{

    public class IPManager
    {
       

        public IPManager()
        {

            string[] ipAndMac = GetLocalMacAndIP(VRCPRServer.Models.ADDRESSFAM.IPv4);

        }

        /// <summary>
        /// 获取本机IP和Mac地址
        /// </summary>
        /// <param name="Addfam"></param>
        /// <returns></returns>
        public static string[] GetLocalMacAndIP(VRCPRServer.Models.ADDRESSFAM Addfam)
        {
            //Return null if ADDRESSFAM is Ipv6 but Os does not support it
            if (Addfam == VRCPRServer.Models.ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6)
            {
                return null;
            }

            string[] output = new string[2];
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            if (networkInterfaces.Length > 0)
            {
                foreach (NetworkInterface networkInterface in networkInterfaces)
                {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
                    NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;
                    NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet;

                    if ((networkInterface.NetworkInterfaceType == _type1 || networkInterface.NetworkInterfaceType == _type2) && networkInterface.OperationalStatus == OperationalStatus.Up)
#endif
                    {
                        IEnumerable<UnicastIPAddressInformation> unicastIPs = networkInterface.GetIPProperties().UnicastAddresses;
                        foreach (UnicastIPAddressInformation unicastIPAddress in unicastIPs)
                        {
                            //IPv4
                            if (Addfam == VRCPRServer.Models.ADDRESSFAM.IPv4)
                            {
                                if (unicastIPAddress.Address.AddressFamily == AddressFamily.InterNetwork)
                                {
                                    PhysicalAddress physicalAddress = networkInterface.GetPhysicalAddress();
                                    output[0] = networkInterface.GetPhysicalAddress().ToString().ToLower();
                                    output[1] = unicastIPAddress.Address.ToString();
                                    return output;
                                }
                            }
                            //IPv6
                            else if (Addfam == VRCPRServer.Models.ADDRESSFAM.IPv6)
                            {
                                if (unicastIPAddress.Address.AddressFamily == AddressFamily.InterNetworkV6)
                                {
                                    output[0] = networkInterface.GetPhysicalAddress().ToString();
                                    output[1] = unicastIPAddress.Address.ToString();
                                    return output;
                                }
                            }
                        }
                    }

                }
            }
            else
            {
                IPAddress[] iPAddresses = Dns.GetHostAddresses(Dns.GetHostName());

                if (iPAddresses != null)
                {                  
                    output[0] = "";

                     output[1] = iPAddresses[0].ToString();
                }
            }
            return output;
        }

    }


}



